package com.xiaoyu.izf.service;

import com.xiaoyu.izf.model.GoodsInfo;
import com.xiaoyu.izf.model.page.DataTableBean;
import com.xiaoyu.izf.model.page.ParamListBean;
import com.xiaoyu.izf.repository.GoodsInfoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.sql.Timestamp;

@Service
public class GoodsService extends BaseService{
    @Autowired
    private GoodsInfoDao goodsInfoDao;

    public DataTableBean list(ParamListBean param){
        PageRequest pageRequest = buildPageRequestSort(param.getPageNo(), param.getPageLength(), "id",param.getFieldName(),param.getCompositorType());
        Page<GoodsInfo> page = goodsInfoDao.findAll((root, query, cb) -> {
            Predicate predicate = cb.equal(root.get("isDel"), 0);
            return predicate;
        },pageRequest);
        DataTableBean data = new DataTableBean();
        data.setsEcho(param.getsEcho());
        data.setiTotalDisplayRecords((int)page.getTotalElements());
        data.setiTotalRecords((int)page.getTotalElements());
        data.setAaData(page.getContent());
        return data;
    }

    public void goodsInfoDel(int id){
        GoodsInfo goods = goodsInfoDao.findOneById(id);
        goods.setIsDel(1);
        goodsInfoDao.save(goods);
    }

    public void goTop(int id){
        GoodsInfo goods = goodsInfoDao.findOneById(id);
        goods.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        goodsInfoDao.save(goods);
    }

    public GoodsInfo oneById(int id){
        return goodsInfoDao.findOneById(id);
    }

    public GoodsInfo save(GoodsInfo goodsInfo){
        return goodsInfoDao.save(goodsInfo);
    }
}
